//
//  newsViewController.m
//  项目一
//
//  Created by nick on 15/7/17.
//  Copyright (c) 2015年 nick. All rights reserved.
//

#import "newsViewController.h"
#define sheight [UIScreen mainScreen].bounds.size.height
#define swidth [UIScreen mainScreen].bounds.size.width
#define sNavHeight 64
#define sTabBarHeight 49
#import "newsCell.h"
#import "DataService.h"
#import "newsModal.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "newsListViewController.h"
@interface newsViewController (){
    NSMutableArray *_newsModalArray;
    UITableView *tv;
    UIImageView *headImageView;
    UILabel *imageText;
}

@end

@implementation newsViewController
    

#pragma mark - tools
- (void)dataLoad{
    
    NSArray *dataArray = [DataService setDataWithJason:@"news_list"];
    _newsModalArray = [NSMutableArray new];
    for (NSDictionary *dic in dataArray) {
        newsModal *newsData = [[newsModal alloc]init];
        [newsData setValuesForKeysWithDictionary:dic];
        [_newsModalArray addObject:newsData];
        
        
        
    }
    
}
#pragma mark - createsubView
- (void)_createHeaderView{
    newsModal *nm = [newsModal new];
    nm = _newsModalArray[0];
    
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];
    header.backgroundColor = [UIColor clearColor];
    tv.tableHeaderView = header;
    
    headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, swidth, 200)];
    NSURL *url = [NSURL URLWithString:nm.image];
    [headImageView sd_setImageWithURL:url];
    
    [self.view addSubview:headImageView];
    
    imageText = [[UILabel alloc]initWithFrame:CGRectMake(5, 200-40, swidth, 35)];
    imageText.backgroundColor = [UIColor blackColor];
    imageText.textColor = [UIColor whiteColor];
    imageText.text = nm.title;
    imageText.alpha = 0.8;
    [self.view addSubview:imageText];

    [_newsModalArray removeObject:_newsModalArray[0]];
    
}
- (void)_createTableView{
    tv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, swidth, sheight-64-49) style:UITableViewStylePlain];
    tv.dataSource = self;
    tv.delegate = self;
    [self.view addSubview:tv];
    UINib *nib = [UINib nibWithNibName:@"newsCell" bundle:[NSBundle mainBundle]];
    [tv registerNib:nib forCellReuseIdentifier:@"cell"];
}
#pragma mark - doCelldatasourceAndDeleganta
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    newsListViewController *vc = [[newsListViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    newsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    if (cell == nil) {
//        cell = [[newsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    }
    newsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell setModal:_newsModalArray[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
#pragma mark - didLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createTableView];
    [self dataLoad];
    [self _createHeaderView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - tools
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offy = tv.contentOffset.y;
    if (offy>0) {
        headImageView.frame = CGRectMake(0, -offy, swidth, 200);
        imageText.bottom = headImageView.bottom;
        
    }
    else{
        headImageView.frame = CGRectMake(0+offy, 0, swidth-offy*swidth/200, 200-offy);
        imageText.bottom = headImageView.bottom;
    }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
